<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
*/

class User extends MY_Controller{

	public function __construct() {
		parent::__construct();
		$this->load->model('Userinfo_model','user');
		$this->load->model('Category_model','c');
		$this->load->library('form_validation');
		if ($this->isAdmin() == false) redirect("admin/login"); 
	}

	public function check_username($user_name)
	{

		$userinfo = $this->user->O(array('username'=>$user_name));
		if (count($userinfo))
		{
			$this->form_validation->set_message('check_username', $this->lang->line('error_username_oc'));
			return false;
		}
		else
		{
			return true;
		}
	}

	public function check_email($email)
	{

		$userinfo = $this->user->O(array('email'=>$email));
		if (count($userinfo))
		{
			$this->form_validation->set_message('check_email', $this->lang->line('error_email_oc'));
			return false;
		}
		else
		{
			return true;
		}
	}

	public function admin_menu()
	{
		$this->load->model('admin/Action_model', 'action');

		$adminfo = $this->isAdmin();
		if ($adminfo['action_list'] == 'all' || $adminfo['action'] == '')
		{
			$menu_list = $this->action->L(array('level'=>1),'*',0,0,'sort');
			
			//print_r($menu_list);
			foreach ($menu_list as $k => $v)
			{
				$menu_list[$k]['menu_2'] = $this->action->L(array('parent_id'=>$v['action_id'],'level'=>'2'),'*',0,0,'sort');

				foreach ($menu_list[$k]['menu_2'] as $k2 => $v2)
				{
					$menu_list[$k]['menu_2'][$k2]['menu_3'] = $this->action->L(array('parent_id'=>$v2['action_id'],'level'=>'3'),'*',0,0,'sort');
				}
			}

			return $menu_list;
		}
		
		$action = explode(",",$adminfo['action']);
		foreach ($action as $k => $v)
		{
			$level3_t = $this->action->O(array('action_code'=>$v,'level'=>'3'));
			$level3[] = $level3_t['action_id'];
			$level2_t = $this->action->O(array('action_id'=>$level3_t['parent_id'],'level'=>'2'));
			$level2[] = $level2_t['action_id'];
			$level1_t = $this->action->O(array('action_id'=>$level2_t['parent_id'],'level'=>'1'));
			$level1[] = $level1_t['action_id'];
		}
		$level1 = array_unique($level1);
		$level2 = array_unique($level2);
		$level3 = array_unique($level3);
		$menu_list = $this->action->L(array('level'=>1,'action_id in (' . implode(",",$level1) . ')'=>''));

		foreach ($menu_list as $k => $v)
		{
			$menu_list[$k]['menu_2'] = $this->action->L(array('parent_id'=>$v['action_id'],'level'=>'2','action_id in (' . implode(",",$level2) . ')'=>''),'*',0,0,'sort');

			foreach ($menu_list[$k]['menu_2'] as $k2 => $v2)
			{
				$menu_list[$k]['menu_2'][$k2]['menu_3'] = $this->action->L(array('parent_id'=>$v2['action_id'],'level'=>'3','action_id in (' . implode(",",$level3) . ')'=>''),'*',0,0,'sort');
			}
		}
			//print_r($menu_list);
			//exit;
		return $menu_list;
	
	}

	public function setmain()
	{
//		if ($this->isAdmin() == false) redirect("admin/user/login"); 

		$this->_d['adminfo'] = $this->session->userdata('adminfo');

		$this->_d['menu_list'] = $this->admin_menu();
		///print_r($this->_d['menu_list']);
		$this->load->view($this->_d['cfg']['tpl__admin'] . 'index/index', $this->_d);
	}

	public function setmenu()
	{
		if ($this->isAdmin() == false) redirect("admin/user/login"); 

		$this->_d['adminfo'] = $this->isAdmin();
		$this->load->view('admin/public/menu', $this->_d);	
	}

	public function setBegin()
	{
		if ($this->isAdmin() == false) redirect("admin/user/login"); 

		$this->_d['adminfo'] = $this->isAdmin();
		$this->load->view('admin/public/welcome', $this->_d);	
	}


	public function logout()
	{
		parent::adminLogout();
		redirect("admin/login");
	}

}